package view
{
	import model.Seance;
	import model.proxy.SeanceProxy;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	import view.components.SeancePanel;
	import view.components.calendar.CalendarEditionPanel;
	import view.components.calendar.CalendarPanel;
	import view.components.calendar.DocEvent;

	public class SeancePanelMediator extends Mediator implements IMediator 
	{ 
		
		public static const NAME : String = 'CalendarPanelMediator'; 
			
		/** 
		 * Constructor 
		 * Listen event of the component to send notification 
		 */ 
		 public function SeancePanelMediator(viewComponent : SeancePanel) 
		 { 
		 	super(NAME, viewComponent);
		 	viewComponent.addEventListener(CalendarPanel.OBTENIR_LISTE_SEANCES, obtenirListeSeancesNotification);
		 	viewComponent.addEventListener(CalendarPanel.CREER_SEANCE, creerSeance);
		 	viewComponent.addEventListener(CalendarPanel.MODIFIER_SEANCE, modifierSeance);
		 	viewComponent.addEventListener(CalendarPanel.SUPPRIMER_SEANCE, supprimerSeance);
		 	viewComponent.addEventListener(CalendarPanel.PUBLIER_SEANCE, publierSemaine);
		 	
		 	viewComponent.addEventListener(CalendarEditionPanel.OBTENIR_LISTE_MATIERES_CALENDAR, obtenirListeMatieres);
			viewComponent.addEventListener(CalendarEditionPanel.OBTENIR_LISTE_SERVICES_CALENDAR, obtenirListeServices);
			viewComponent.addEventListener(CalendarEditionPanel.OBTENIR_LISTE_GROUPES_CALENDAR, obtenirListeGroupes);
			viewComponent.addEventListener(CalendarEditionPanel.OBTENIR_LISTE_SALLES_CALENDAR, obtenirListeSalles);
		 	viewComponent.addEventListener(CalendarEditionPanel.ITEM_EDITED, modifierSeanceFromPopUp);
		 } 
		 	 
		 private function obtenirListeSeancesNotification(event : DocEvent):void 
		 { 
			sendNotification(CalendarPanel.OBTENIR_LISTE_SEANCES, event.data);
		 } 
		 
		 private function creerSeance(event : DocEvent):void
		 {
			sendNotification(CalendarPanel.CREER_SEANCE, event.data as Seance);
		 }
		 
		 private function modifierSeance(event : DocEvent):void
		 {
			sendNotification(CalendarPanel.MODIFIER_SEANCE, event.data as Seance);
		 }
		 
		 private function supprimerSeance(event : DocEvent):void
		 {
		 	sendNotification(CalendarPanel.SUPPRIMER_SEANCE, event.data as Seance);
		 }
		 
		 private function publierSemaine(event : DocEvent):void
		 {
		 	sendNotification(CalendarPanel.PUBLIER_SEANCE, event.data);
		 }
		 /*********************** POP-UP ***********************/
		 private function obtenirListeMatieres(event : DocEvent):void
		 {
		 	sendNotification(CalendarEditionPanel.OBTENIR_LISTE_MATIERES_CALENDAR, event.data);
		 }

		 private function obtenirListeServices(event : DocEvent):void
		 {
		 	sendNotification(CalendarEditionPanel.OBTENIR_LISTE_SERVICES_CALENDAR, event.data);
		 }
		 
		 private function obtenirListeGroupes(event : DocEvent):void
		 {
		 	sendNotification(CalendarEditionPanel.OBTENIR_LISTE_GROUPES_CALENDAR, event.data);
		 }
		 
		 private function obtenirListeSalles(event : DocEvent):void
		 {
		 	sendNotification(CalendarEditionPanel.OBTENIR_LISTE_SALLES_CALENDAR, event.data);
		 }		 
		 
		 private function modifierSeanceFromPopUp(event : DocEvent):void
		 {
		 	sendNotification(CalendarPanel.MODIFIER_SEANCE, event.data as Seance);
		 }
		 
		 
		 
		 
		 /********************** RETOUR **************************/
		 override public function listNotificationInterests():Array 
		 { 
		 	return [ SeanceProxy.LISTE_SEANCES_OBTENUE,
		 			 SeanceProxy.LISTE_SEANCES_MAJ,
		 			 SeanceProxy.LISTE_MATIERES_SEANCE_OBTENUE,
		 			 SeanceProxy.LISTE_SERVICES_SEANCE_OBTENUE,
		 			 SeanceProxy.LISTE_GROUPES_SEANCE_OBTENUE,
		 			 SeanceProxy.LISTE_SALLES_SEANCE_OBTENUE,
		 			 SeanceProxy.SEMAINE_PUBLIEE ]; 
		 } 
		 
		 /** 
		 * Actions done when a notifications is catched 
		 */ 
		 override public function handleNotification(note : INotification):void 
		 { 
		 	 var result:Object;
		 	 var popUp:CalendarEditionPanel;
		 	 var calendar:CalendarPanel;
		 	 
			 switch (note.getName()) 
			 { 
			 	case SeanceProxy.LISTE_SEANCES_OBTENUE:
			 	{
			 		result = note.getBody();
			 		calendar = result["calendarPanel"];
			 		calendar.items = new ArrayCollection(result["listeSeances"] as Array);
			 	}
			 	break;
			 	
			 	case SeanceProxy.LISTE_SEANCES_MAJ:
			 	{
			 		calendarPanel.items = new ArrayCollection(note.getBody() as Array);
			 	}
			 	break;
			 	
			 	case SeanceProxy.LISTE_MATIERES_SEANCE_OBTENUE:
			 	{
			 		result = note.getBody();
			 		popUp = result["PopUp"];
			 		popUp.coursesList.dataProvider = result["ListeMatieres"];
			 	}
			 	break;
			 	
			 	case SeanceProxy.LISTE_SERVICES_SEANCE_OBTENUE:
			 	{
			 		result = note.getBody();
			 		popUp = result["PopUp"];
			 		popUp.servicesList.enabled = true;
			 		popUp.servicesList.dataProvider = result["ListeServices"];
			 		popUp.onServicesListLoaded();
			 	}
			 	break;
			 	
			 	case SeanceProxy.LISTE_GROUPES_SEANCE_OBTENUE:
			 	{
			 		result = note.getBody();
			 		popUp = result["PopUp"];
			 		popUp.groupsList.dataProvider = result["ListeGroupes"];
			 		popUp.onGroupsListLoaded();
			 	}
			 	break;
			 	
			 	case SeanceProxy.LISTE_SALLES_SEANCE_OBTENUE:
			 	{
			 		result = note.getBody();
			 		popUp = result["PopUp"];
			 		popUp.roomsList.dataProvider = result["ListeSalles"];
			 		popUp.onRoomsListLoaded();
			 	}
			 	break;
			 	
			 	case SeanceProxy.SEMAINE_PUBLIEE:
			 	{
			 		Alert.show("La semaine a été publiée.");
			 	}
			 	break;
			 	
			 	default:
			 	break;
		 	} 
		 } 
		 
		 private function get seancePanel():SeancePanel
		 {
		 	return viewComponent as SeancePanel;
		 }
		 
		 private function get calendarPanel():CalendarPanel
		 { 
		 	return seancePanel.viewStack.selectedChild as CalendarPanel; 
		 }
	}
}